<template>
  <!-- 搜索工作栏 -->
  <ContentWrap ref="form">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!--      <el-form-item label="老师ID" prop="teacherId">-->
      <!--        <el-input-->
      <!--          v-model.tirm="queryParams.teacherId"-->
      <!--          placeholder="请输入老师ID"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="班级ID" prop="classId">-->
      <!--        <el-input-->
      <!--          v-model.tirm="queryParams.classId"-->
      <!--          placeholder="请输入班级ID"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="关系" prop="relation">-->
      <!--        <el-input-->
      <!--          v-model.tirm="queryParams.relation"-->
      <!--          placeholder="请输入关系"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="状态" prop="status">-->
      <!--        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">-->
      <!--          <el-option label="请选择字典生成" value="" />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="阶段编号" prop="stageId">
        <el-input
          v-model.tirm="queryParams.stageId"
          placeholder="请输入阶段编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="开始年度" prop="beginYear">-->
      <!--        <el-input-->
      <!--          v-model.tirm="queryParams.beginYear"-->
      <!--          placeholder="请输入开始年度"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="开始时间" prop="beginDate">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.beginDate"-->
      <!--          value-format="YYYY-MM-DD HH:mm:ss"-->
      <!--          type="daterange"-->
      <!--          start-placeholder="开始日期"-->
      <!--          end-placeholder="结束日期"-->
      <!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="结束结束" prop="endDate">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.endDate"-->
      <!--          value-format="YYYY-MM-DD HH:mm:ss"-->
      <!--          type="daterange"-->
      <!--          start-placeholder="开始日期"-->
      <!--          end-placeholder="结束日期"-->
      <!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="创建时间" prop="createTime">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.createTime"-->
      <!--          value-format="YYYY-MM-DD HH:mm:ss"-->
      <!--          type="daterange"-->
      <!--          start-placeholder="开始日期"-->
      <!--          end-placeholder="结束日期"-->
      <!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm"
          v-hasPermi="['cmis:teacher-class:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          @click="handleDeleteMulti"
          v-hasPermi="['cmis:teacher-class:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap :setPadding="false">
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="selectionChange"
      stripe
      :max-height="tableHeight"
    >
      <el-table-column type="selection" width="55" />
      <!--      <el-table-column label="编号" prop="id" />-->
      <!--      <el-table-column label="老师ID" prop="teacherId" />-->
      <!--      <el-table-column label="班级ID" prop="classId" />-->
      <el-table-column label="班级名称" prop="classesName" show-overflow-tooltip />
      <!--      <el-table-column label="关系" prop="relation" />-->
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="阶段编号" prop="stageId" show-overflow-tooltip />
      <el-table-column label="开始年度" prop="beginYear" show-overflow-tooltip />
      <el-table-column
        label="开始时间"
        prop="beginDate"
        :formatter="dateFormatter"
        show-overflow-tooltip
      />
      <el-table-column
        label="结束结束"
        prop="endDate"
        :formatter="dateFormatter"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cmis:teacher-class:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：绑定 -->
  <TeacherClassBindForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="TeacherClass">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as TeacherClassApi from '@/api/cmis/teacherClass'
import TeacherClassBindForm from './TeacherClassBindForm.vue'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const route = useRoute()
const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  teacherId: null,
  classId: null,
  relation: null,
  status: null,
  stageId: null,
  beginYear: null,
  beginDate: [],
  endDate: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const teacherStateRef = ref() // 老师信息
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TeacherClassApi.getTeacherClassPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = () => {
  formRef.value.open(teacherStateRef.value)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TeacherClassApi.deleteTeacherClass(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const listOfIds = ref([]) //选中的设备

// 选中设备
const selectionChange = (val: any) => {
  listOfIds.value = val.map((item: any) => item.id)
}

// 批量删除操作
const handleDeleteMulti = async () => {
  if (listOfIds.value.length > 0) {
    try {
      // 批量删除的二次确认
      await message.delConfirm()
      // 发起批量删除
      await TeacherClassApi.batchDeleteTeacher(listOfIds.value)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  } else {
    message.warning(t('请选择要删除的班级!'))
  }
}

/** 初始化 **/
onMounted(() => {
  const { teacherId, teacherSchoolId, teacherSchoolName } = route.query
  console.log('list.teacherId', teacherId)
  teacherStateRef.value = [teacherId, teacherSchoolId, teacherSchoolName]
  queryParams.teacherId = teacherId
  getList()
})
</script>
